package cn.ultrarisk.scadmin.manager.web.controller;

import cn.ultrarisk.scadmin.manager.biz.user.dao.AdminDAO;
import cn.ultrarisk.scadmin.manager.biz.user.dao.PrivilegeDAO;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Admin;
import cn.ultrarisk.scadmin.manager.biz.user.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * LoginController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {
    @Autowired
    private AdminDAO adminDAO;
    @Autowired
    private PrivilegeDAO privilegeDAO;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Map<String, Object>> login(String account, String password) {
        Map<String, Object> map = new HashMap<String, Object>();
        Admin admin = adminDAO.getByAccount(account);
        if (admin != null) {
            if (Admin.ROOT_ACCOUNT.equals(admin.getAccount()) && Admin.ROOT_PASSWORD.equals(password)) {
                SessionUtil.setAdmin(admin);
                map.put("flag", true);
                return getOkResponseEntity(map);
            }
            try {
//                Integer ldapLoginStatus = ldapUserService.authLogin(admin.getAccount(), password);
//                if (ldapLoginStatus != null && ldapLoginStatus == 1) {
//                    SessionUtil.setAdmin(admin);
//                    map.put("flag", true);
//                    return getOkResponseEntity(map);
//                }
            } catch (Exception ignored) {
            }
            map.put("flag", false);
            map.put("msg", "账号或密码错误");
            return getOkResponseEntity(map);
        }
        map.put("flag", false);
        map.put("msg", "账号不存在");
        return getOkResponseEntity(map);
    }
}
